package zdy.web.control;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import zdy.web.bean.book;

/**
 * Servlet implementation class Ajaxservice1
 */
@WebServlet("/s/ajax")
public class Ajaxservice1 extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Ajaxservice1() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setCharacterEncoding("UTF-8");
		book book1 = new book("Think in Java", 100);
		book book2 = new book("java 入门", 50);
		book book3 = new book("oracle 入门", 100);
		try {
			List<book> Lis = new ArrayList<book>();
			Lis.add(book1);
			Lis.add(book2);
			Lis.add(book3);
			String str = JSON.toJSONString(Lis);
			System.out.println(str);
			PrintWriter out = response.getWriter();
			out.println(str);
			out.close();
			// response.getWriter().append(str);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
